<template>
<!-- 头部区域 -->
     <el-container class="home_container">
        <el-header>
          <div class="logorose">
            <img src="../assets/logorose.jpg">
            <span>电商管理系统</span>
          </div>
          <el-button type='primary' @click="logout">退出</el-button>
        </el-header>
     <el-container>
       <!-- 左侧边区 -->
          <el-aside :width="iscollapse ? '64px' : '200px'">
            <div class="toggle-button" @click='toggleTab'>|||</div>

            <el-menu
             background-color="#333744"
             text-color="#fff"
             active-text-color="#409bff"
             unique-opened :collapse="iscollapse" :collapse-transition="false"
             :router="true"
             :default-active="activepath">
             <!-- 一级菜单 -->
             <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
               <!-- 一级菜单模板区 -->
               <template slot="title">
                 <i :class="iconsObj[item.id]"></i>
                 <span>{{item.authName}}</span>
               </template>
               <!-- 二级菜单 -->
               <!-- index属性就是跳转的路由 -->
               <el-menu-item @click="saveNavState('/'+subItem.path)" :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id">
                 <!-- 二级菜单模板区 -->
                 <template slot="title">
                   <i class="el-icon-menu"></i>
                   <span>{{subItem.authName}}</span>
                 </template>
               </el-menu-item>
             </el-submenu>
           </el-menu>
          </el-aside>
          <!-- 右侧区 -->
          <el-main>
            <router-view></router-view>
          </el-main>
        </el-container>
     </el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      iscollapse: false,
      activepath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activepath = window.sessionStorage.getItem('activepath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      console.log(this.menulist)
    },
    // 切换菜单折叠展开
    toggleTab () {
      this.iscollapse = !this.iscollapse
    },
    // 保存二级链接高亮状态
    saveNavState (activepath) {
      window.sessionStorage.setItem('activepath', activepath)
      this.activepath = activepath
    }
  }
}
</script>
<style lang="less" scoped>
.logorose {
  width: 100%;
  height: 60px;
  img {
    height: 100%;
  }
}
.home_container {
  height: 100%;
}
.el-header {
  padding-left: 0;
  color: #EAEDF1;
  font-size: 20px;
  background-color: black;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 18px;
    }
  }
}
.el-aside {
  color: #EAEDF1;
  background-color: #333744;
  .el-menu {
    border-right: 1px;
  }
}
.el-main {
  background-color: #EAEDF1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 28px;
  color: #EAEDF1;
  letter-spacing: 0.3em;
  text-align: center;
  cursor: pointer;
}
</style>
